import type { UpdateFormChangeType, UpdateWrapper } from "@/types/wrappertypes";

//普通表单比对差以
export const getChangedFields = <T extends object>(current: T, original: T): UpdateFormChangeType => { // 修正返回类型为 Partial<T>
  const changes: UpdateFormChangeType = {
    _up_n:[],
    _up_o:[],
    _up_field:[],
  };
  (Object.keys(current) as Array<keyof T>).forEach(key => {
    debugger
    if (current[key] !== original[key]) {
      changes._up_n?.push(current[key] as string);
      changes._up_o?.push(original[key] as string);
      changes._up_field?.push(key as string);
    }
  });
  console.warn("updateChangesCheck:",changes);
  return changes;
};




// 深度对比函数(递归) 先放着，没想好返回用什么数据结构接。
export const deepGetChangedFields = <T extends object>(current: T, original: T): Partial<T> => {
  const changes: Partial<T> = {};
  (Object.keys(current) as Array<keyof T>).forEach((key) => {
    const currentVal = current[key];
    const originalVal = original[key];
    if (typeof currentVal === 'object' && currentVal !== null && typeof originalVal === 'object' && originalVal !== null) {
      const nestedChanges = deepGetChangedFields(currentVal, originalVal);
      if (Object.keys(nestedChanges).length > 0) {
        changes[key] = nestedChanges as T[keyof T];
      }
    } else if (currentVal !== originalVal) {
      changes[key] = currentVal;
    }
  });
  console.log("updateChangesCheck_deep:",changes)
  return changes;
};
export const updateWrapperBuilder = (data: object, upwrapper: UpdateFormChangeType): UpdateWrapper => {
  const res: UpdateWrapper = {
    u_data: data || null,
    _up_wrapper: upwrapper || null,
  };
  return res; // 添加返回值以修复错误
};
